package com.spring.cloud.practice.demo.eurekaclientribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * <b><code>BookRibbonController</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 2019/8/1 16:18.
 *
 * @author huanghuayan
 * @since spring-cloud-demo
 */
@RestController
public class BookRibbonController {

    private Logger logger = LoggerFactory.getLogger(BookRibbonController.class);

    @Value("${server.port}")
    String port;

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("getBook")
    @HystrixCommand(fallbackMethod = "getBookError")
    public String getBook(@RequestParam String name) {
        logger.info("name:", name);
        return restTemplate.getForObject("http://eureka-client/getBook?name="+name,String.class);
        //return "The name of the book is " + name + ", i am from port:" + port;
    }

    public String getBookError(String name) {
        return "The name of the book is " + name + ", get Book Error!!";
    }
}
